package edu.scsu.csci.context;


import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import edu.scsu.csci.IView;
import edu.scsu.csci.annotation.AView;
import edu.scsu.csci.core.ActionMethod;

/**
 * Holds the context infomration for the view to use.
 *  
 * @author Maqbool Khan
 * @since jdk 1.5
 * @see
 */
/**
 *
 * @author Maqbool Khan
 * @since jdk 1.5
 * @see
 * @viz.diagram ViewContext.tpx
 */
public class ViewContext	{
	/**
	 * servlet request.
	 */
	private ServletRequest request;
	
	/**
	 * servlet response.
	 */
	private ServletResponse response;
		
	/**
	 * resouce name.
	 * 
	 * Can be jap name, xslt style sheet name or velocity page name, etc.
	 */
	private final String resourceName;
	
	/**
	 * resource url.
	 */
	private final String resourceLocation;
	
	/**
	 * error resource name.
	 */
	private final String errorResourceName;
	
	/**
	 * error resource name.
	 */
	private final String errorResourceLocation;
	
	/**
	 * view implementation class.
	 */
	private final IView viewStratergy; 
	
	
	/**
	 * Creates an instance of view context.
	 * 
	 * @param resouceLocation
	 * @param resouceName
	 * @param errorResouceLocation
	 * @param errorResourceName
	 * @param viewStratergy
	 * @param request
	 * @param response
	 */
	public ViewContext(String resouceLocation, String resouceName, String errorResouceLocation, String errorResourceName, 
				IView viewStratergy, ServletRequest request, ServletResponse response) {
		this.resourceLocation = resouceLocation;
		this.resourceName = resouceName;
		this.errorResourceLocation = errorResouceLocation;
		this.errorResourceName = errorResourceName;
		this.viewStratergy = viewStratergy;
		this.request = request;
		this.response = response;
		
	}

	/**
	 * @param request
	 * @param response
	 * @return
	 */
	public static ViewContext getInstance(ServletRequest request, ServletResponse response, 
				ActionMethod commandMethod, Object businessObject){
		AView viewAnnotation = (AView) commandMethod.getAnnotation(AView.class);
		//TODO: Make sure you read the view annotation from the class if none is defined for the method.
		if(viewAnnotation != null){
			IView viewClazz = IView.ViewFactory.getInstance(viewAnnotation.viewImlementation());
			return new ViewContext(viewAnnotation.resourceLocation(), viewAnnotation.resourceName(),
						viewAnnotation.errorResourceLocation(), viewAnnotation.errorResourceName(), viewClazz, 
						request, response);
		}
		
		viewAnnotation = (AView) businessObject.getClass().getAnnotation(AView.class);
		if(businessObject != null){
			IView viewClazz = IView.ViewFactory.getInstance(viewAnnotation.viewImlementation());
			return new ViewContext(viewAnnotation.resourceLocation(), viewAnnotation.resourceName(),
						viewAnnotation.errorResourceLocation(), viewAnnotation.errorResourceName(), viewClazz, 
						request, response);
		}
		return null;
	}

	/**
	 * @return
	 */
	public String getErrorResourceLocation() {
		return errorResourceLocation;
	}

	/**
	 * @return
	 */
	public String getErrorResourceName() {
		return errorResourceName;
	}

	/**
	 * @return
	 */
	public ServletRequest getRequest() {
		return request;
	}

	/**
	 * @return
	 */
	public String getResourceLocation() {
		return resourceLocation;
	}

	/**
	 * @return
	 */
	public String getResourceName() {
		return resourceName;
	}

	/**
	 * @return
	 */
	public ServletResponse getResponse() {
		return response;
	}
	
	/**
	 * @return
	 */
	public IView getViewStratergy() {
		return viewStratergy;
	}

}
